package baseFx.database.mybatis;

import baseFx.database.CursorReader;
import org.apache.ibatis.cursor.Cursor;

import java.io.IOException;
import java.util.Iterator;

public class MybatisCursorReader<T> implements CursorReader<T> {
    private Cursor cursor;
    private Iterator iterator;

    public MybatisCursorReader(Cursor cursor) {
        this.cursor = cursor;
        this.iterator = cursor.iterator();
    }

    @Override
    public T next() {
        if (iterator.hasNext()) {
            return (T) iterator.next();
        }
        return null;
    }


    @Override
    public void close() {
        if (cursor != null) {
            iterator = null;
            try {
                cursor.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
